import { parse } from 'url';
import { TableListItem, TableListParams } from './data.d';

function getPartitionManagement(
  req: { url: any },
  res: {
    json: (
      arg0: { body: { list: TableListItem[]; count: number; } },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const params = (parse(new_url, true).query as unknown) as TableListParams;

  const { pageIndex, pageSize } = params;
  const starNum = (pageIndex - 1) * pageSize;
  const endNum = pageIndex * pageSize;

  const dataSource: TableListItem[] = [];

  [...new Array(endNum - starNum)].map((item, index) => {
    dataSource.push({
      id: index,
      type: Math.ceil(Math.random() * 2),
      logisName: `宏源大厦物业${index + 1}`,
      gardenName: `福熙大道${index + 1}`,
      name: `物流中心${index + 1}`,
      status: Math.ceil(Math.random() * 2),
      createUser: '张三丰',
      createTime: '2019-08-27',
      updateUser: '张无忌',
      updateTime: '2019-08-27',
    });
    return dataSource;
  });

  const result = {
    code: 0,
    body: {
      list: dataSource,
      count: 50,
    },
  };

  return res.json(result);
}

export default {
  'GET /logis/region/list': getPartitionManagement,
};
